﻿using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 创建快捷方式
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void createBtn_Click(object sender, RoutedEventArgs e)
        {
            string exePath = this.targetProgramFile.Text.Trim();
            string name = this.shortcutName.Text.Trim();
            string ico = this.shortcutIco.Text.Trim();
            string param = this.shortcutParam.Text.Trim();

            if (string.IsNullOrWhiteSpace(exePath))
            {
                _ = MessageBox.Show("请选择目标程序文件！", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            name = (string.IsNullOrWhiteSpace(name) ? System.IO.Path.GetFileNameWithoutExtension(exePath) : name) + ".lnk";
            param = string.IsNullOrWhiteSpace(param) ? "" : param;
            ico = string.IsNullOrWhiteSpace(ico) ? exePath : ico;

            bool result = CreateDesktopShortcut(exePath, name, ico, param);

            if (result)
            {
                _ = MessageBox.Show("创建快捷方式成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                _ = MessageBox.Show("创建快捷方式失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        #region 创建桌面快捷方式
        private bool CreateDesktopShortcut(string exePath, string name, string icon, string param)
        {
            string deskTop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
            string dirPath = System.IO.Path.GetDirectoryName(exePath);
            //string exePath = Assembly.GetExecutingAssembly().Location;
            System.Diagnostics.FileVersionInfo exeInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(exePath);
            string targetLnk = string.Format(@"{0}\{1}", deskTop, name);
            if (System.IO.File.Exists(targetLnk))
            {
                MessageBoxResult result = MessageBox.Show("存在相同快捷方式，确认是否覆盖？", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Cancel)
                {
                    return false;
                }

                System.IO.File.Delete(targetLnk);
            }
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(targetLnk);
            shortcut.TargetPath = @exePath;         //目标文件
            shortcut.WorkingDirectory = dirPath;    //目标文件夹
            shortcut.WindowStyle = 1;               //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
            //shortcut.Description = "";   //描述
            shortcut.IconLocation = icon;// string.Format(@"{0}\64.ico", dirPath);  //快捷方式图标
            shortcut.Arguments = param;
            //shortcut.Hotkey = "SHIFT+DELETE";              // 快捷键
            shortcut.Save();

            return true;
        }
        #endregion

        private void selectExeBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "程序文件|*.exe;";
            openFileDialog.CheckPathExists = true;
            openFileDialog.CheckFileExists = true;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.targetProgramFile.Text = openFileDialog.FileName;
            }

            openFileDialog.Dispose();
        }

        private void selectIcoBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "程序文件|*.exe|" +
                                    "图标文件|*.ico";
            openFileDialog.CheckPathExists = true;
            openFileDialog.CheckFileExists = true;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.shortcutIco.Text = openFileDialog.FileName;
            }

            openFileDialog.Dispose();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string[] files = System.IO.Directory.GetFiles(path, "*.exe");
            string file = files.FirstOrDefault(f =>
            {
                var fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(f);
                return fileInfo.CompanyName.Equals("mjsoftware", StringComparison.OrdinalIgnoreCase) &&
                fileInfo.FileDescription.Equals("启动服务", StringComparison.OrdinalIgnoreCase);
            });
            if (!string.IsNullOrWhiteSpace(file))
            {
                this.targetProgramFile.Text = file;
            }
        }
    }
}
